home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ldb.zip / SDDEM1.CPP < prev   
C/C++ Source or Header  |  1991-10-18  |  3KB  |  156 lines

  1.     // sddem1.cpp
  2.     // Demo Streamable Data
  3.     // Link with binder.obj, sbinder.obj, and sdata.obj
  4.  
  5.     #include <string.h>
  6.     #include <fstream.h>
  7.     #include "sdata.hpp"
  8.  
  9.     #define DID_Int    2
  10.  
  11.     #define ID_SDATA_CMP  1
  12.  
  13.  
  14.  
  15.     // display integers and strings
  16.  
  17.     void display(SDatA N)
  18.     {
  19.         cout << endl << "DID: " << setw(6)
  20.             << N->DID()
  21.             << "  sizeofData: " << setw(6)
  22.             << N->SizeofData() << "  D:  ";
  23.         switch (N->DID())  {
  24.         case DID_Generic:
  25.             cout << "unknown";
  26.             break;
  27.         case DID_String:
  28.             cout << (char *)(voiD) * N;
  29.             break;
  30.         case DID_Int:
  31.             cout << *(int *)(voiD) * N;
  32.             break;
  33.         }
  34.  
  35.     }
  36.  
  37.  
  38.     // sort integers and strings
  39.  
  40.     int sdcmp(SDatA N1, SDatA N2)
  41.     {
  42.         // integers sorted to the front
  43.         // strings sorted to the rear
  44.  
  45.         if (N1->DID() == DID_Generic)
  46.             return 1;
  47.         else if (N2->DID() == DID_Generic)
  48.             return -1;
  49.         if (N1->DID() == DID_Int)
  50.             if (N2->DID() == DID_Int)
  51.                 return *(int *)(voiD)*N1
  52.                     - *(int *)(voiD)*N2;
  53.             else
  54.                 return -1;
  55.         else
  56.             if (N2->DID() == DID_Int)
  57.                 return 1;
  58.             else
  59.                 return strcmp((const char *)
  60.                     (voiD)*N1,
  61.                     (const char *)
  62.                     (voiD)*N2);
  63.     }
  64.  
  65.  
  66.     main()
  67.     {
  68.         SBinder::registerClass();
  69.         SData::registerClass();
  70.  
  71.  
  72.         SBinder B;
  73.  
  74.         B.push(new SData("Hello LDB!"));
  75.         B.insQ(new SData("Goodbye linked"));
  76.         B.insQ(new SData("list programming!"));
  77.         B.insQ(B.bottom());
  78.         B.insQ(new SData(
  79.             "Line above tests multilinking!"));
  80.  
  81.         for (int i = 3; i; i--)
  82.             B.insQ(new SData(&i,sizeof(i),
  83.                 DID_Int));
  84.  
  85.         cout << "\n\nBinder of streamable integers"
  86.             << " and strings!\n\n";
  87.  
  88.         B.forEach((BDRforEachBlocK)display);
  89.  
  90.         cout << "\n\nPress enter to continue ...";
  91.         cin.get();
  92.  
  93.  
  94.         B.setComparE((BDRcomparE)sdcmp);
  95.         
  96.         B.link();
  97.  
  98.         RegisterFunction(ID_SDATA_CMP,
  99.             (GenericFnC)sdcmp);
  100.  
  101.         ofstream oS("sddem1.txt");
  102.         if (oS)  {
  103.  
  104.           oS << (StreamablE) B;
  105.  
  106.           B.restream();
  107.  
  108.           B.allFree();
  109.  
  110.           // Don't stream B again
  111.           // without restreaming!!!
  112.  
  113.           oS.close();
  114.           ifstream iS("sddem1.txt");
  115.           if (iS)  {
  116.  
  117.             StreamablE C;
  118.  
  119.             cout << "\nStreamed and "
  120.             << "reloaded Binder with "
  121.             << "multiple links maintained "
  122.             << "\nand sorted with streamed "
  123.             << "compare fnc, ints in front:"
  124.             << " \n";
  125.  
  126.             iS >> C;
  127.             iS.close();
  128.  
  129.             RestreamRegistry();
  130.  
  131.             // Don't load again from
  132.             // any stream without
  133.             // restreaming!!!
  134.  
  135.             if (C)
  136.             {
  137.               ((SBindeR)C)->sort();
  138.               ((SBindeR)C)->forEach(
  139.             (BDRforEachBlocK)display);
  140.               delete (SBindeR) C;
  141.             }
  142.             else
  143.               cout << "\n\nUnable to reload"
  144.             << " Binder \n\n";
  145.           }
  146.           else
  147.             cout << "\n\nUnable to reopen"
  148.               << " stream for input of"
  149.               << " of Binder \n\n";
  150.         }
  151.  
  152.         B.unlink();
  153.  
  154.         return 0;
  155.     }
  156.